package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.CompoundWord;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.corpus.document.sentence.word.Word;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ZJGCTokenizer extends BaseTokenizer {


    private static Logger logger = LoggerFactory.getLogger(ZJGCTokenizer.class);

    private ZJGCTokenizer() {
    }

    private static ZJGCTokenizer instance = new ZJGCTokenizer();

    public static ZJGCTokenizer getInstance() {
        return instance;
    }

    public List<String> splitWords(String text) {
        List<String> wordList = new ArrayList<>();
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> words = sentence.wordList;
        IWord iWord = null;
        for (int i = 0; i < words.size(); i++) {
            iWord = words.get(i);
            wordList.add(iWord.getValue().trim());
        }
        return wordList;
    }

    public ExtractItem splitWords(ExtractBizModel extractBizModel, String text) {
        if (extractBizModel == null) {
            return null;
        }
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        ExtractItem extractItem = null;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            if (iWord instanceof CompoundWord) {
                CompoundWord compoundWord = (CompoundWord) iWord;
                List<Word> innerWordList = compoundWord.innerList;
                for (Word word : innerWordList) {
                    String value = word.getValue().trim();
                    if (extractBizModel.getSynonyms().contains(value)) {
                        extractItem = extractBizModel.getExtractItemBySynonym(value);
                        logger.info("分析到在建工程相关信息，IteName：{}，在建工程指标名称：{}。", extractItem.getItemName(), value);
                        break;
                    }
                }
                if (extractItem != null) {
                    break;
                }
            } else {
                String value = iWord.getValue().trim();
                if (extractBizModel.getSynonyms().contains(value)) {
                    extractItem = extractBizModel.getExtractItemBySynonym(value);
                    logger.info("分析到在建工程相关信息，IteName：{}，在建工程指标名称：{}。", extractItem.getItemName(), value);
                    break;
                }
            }
        }
        return extractItem;
    }
}
